package com.example.wenliangjiudian.controller;

import com.example.wenliangjiudian.entity.Customer;
import com.example.wenliangjiudian.entity.Room;
import com.example.wenliangjiudian.entity.RoomType;
import com.example.wenliangjiudian.pojo.RoomDO;
import com.example.wenliangjiudian.service.RoomService;
import com.example.wenliangjiudian.service.RoomTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class RoomController {

    @Autowired
    private RoomService roomService;

    @Autowired
    private RoomTypeService roomTypeService;



    @RequestMapping("/room/inRoomBtn")
    public Object inRoomBtn(String roomNum, HttpServletRequest request) {
        List<Room> roomList = roomService.selectAll();


        request.setAttribute("roomNum", roomNum);
        request.setAttribute("roomList", roomList);

        return "back/in_room";
    }

    @RequestMapping("/room/inRoom")
    public Object inRoom(String roomNum, Customer customer,
                         HttpServletRequest request) {

        boolean b = roomService.inRoom(roomNum, customer);

        //拿到房间列表
        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomList", rooms);
        return "back/home";
    }


    @RequestMapping("/room/outRoomBtn")
    public Object outRoomBtn(String roomNum, HttpServletRequest request) {
        Integer status = 4;
        roomService.updateStatus(roomNum, status);

        //拿到房间列表
        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomList", rooms);

        return "back/home";
    }

    @RequestMapping("/room/clearEndRoomBtn")
    public Object clearEndRoomBtn(String roomNum, HttpServletRequest request) {
        Integer status = 1;
        roomService.updateStatus(roomNum, status);

        //拿到房间列表
        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomList", rooms);

        return "back/home";
    }

    @RequestMapping("/room/bookBtn")
    public Object bookBtn(String roomNum, HttpServletRequest request) {
        Integer status = 2;
        roomService.updateStatus(roomNum, status);

        //拿到房间列表
        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomList", rooms);

        return "back/home";
    }

    @RequestMapping("/room/info")
    public Object info(HttpServletRequest request) {

        List<RoomDO> rooms = roomService.listRoom();

        request.setAttribute("roomlist", rooms);

        return "back/room_info";
    }

    @RequestMapping("/room/deleteRoom")
    public Object deleteRoom(String roomNum, HttpServletRequest request) {
        roomService.deleteRoom(roomNum);

        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomlist", rooms);
        return "back/room_info";
    }

    @RequestMapping("/room/addRoomBtn")
    public Object addRoomBtn(HttpServletRequest request) {
        List<RoomType> roomTypeList = roomTypeService.selectAll();
        request.setAttribute("roomTypeList", roomTypeList);

        return "back/addroom";
    }

    @RequestMapping("/room/addRoom")
    public Object addRoom(String roomNum,Integer roomType,Integer price,HttpServletRequest request) {

        boolean b = roomService.addRoom(roomNum, roomType, price);

        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomlist", rooms);
        return "back/room_info";
    }


    @RequestMapping("/room/updateRoomBtn")
    public Object updateRoomBtn(String roomNum,HttpServletRequest request) {
        Room room = roomService.getRoomByRoomNum(roomNum);
        request.setAttribute("room", room);

        List<RoomType> roomTypeList = roomTypeService.selectAll();
        request.setAttribute("roomTypeList", roomTypeList);


        return "back/updateroom";
    }

    @RequestMapping("/room/updateRoom")
    public Object updateRoom(String roomNum,Integer roomType,Integer price,HttpServletRequest request) {

        boolean b = roomService.updateRoom(roomNum, roomType, price);

        List<RoomDO> rooms = roomService.listRoom();
        request.setAttribute("roomlist", rooms);
        return "back/room_info";
    }
}
